package vn.easyticket.db.facet.Impl;

import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;

import vn.easyticket.db.entity.AbstractEntity;
import vn.easyticket.db.facet.AbstractReposityService;
import vn.easyticket.db.reposity.AbstractEntityReposity;

public abstract class AbstractRepositoryServiceImpl<T extends AbstractEntity, S extends java.io.Serializable>
    implements AbstractReposityService<T, S>
{
    protected abstract AbstractEntityReposity<T, S> getRepository();

    private EntityManagerFactory emf = null;
    
    @PersistenceUnit
    private void setEntityManagerFactory(EntityManagerFactory emf)
    {
        this.emf = emf;
    }
    
    protected EntityManagerFactory getEntityManagerFactory()
    {
        return this.emf;
    }
    
    @Transactional
    public long count()
    {
        return getRepository().count();
    }

    @Transactional
    public void delete( Iterable<? extends T> entities )
    {
        getRepository().delete( entities );
    }

    @Transactional
    public void delete( S id ){
        getRepository().delete( id );
    }

    @Transactional
    public void delete( T entity ){
        getRepository().delete( entity );
    }

    @Transactional
    public void deleteAll(){
        getRepository().deleteAll();
    }

    @Transactional
    public boolean exists( S id ){
        return getRepository().exists( id );
    }

    @Transactional
    public Iterable<T> findAll(){
        return getRepository().findAll();
    }

    @Transactional
    public Iterable<T> findAll( Iterable<S> ids ){
        return getRepository().findAll( ids );
    }

    @Transactional
    public Page<T> findAll( Pageable pageable ){
        return getRepository().findAll( pageable );
    }

    @Transactional
    public Iterable<T> findAll( Sort sort ){
        return getRepository().findAll( sort );
    }

    @Transactional
    public T findOne( S id ){
        return getRepository().findOne( id );
    }

    @Transactional
    public <T_ extends T> Iterable<T_> save( Iterable<T_> entities ){
        return getRepository().save( entities );
    }

    @Transactional
    public <U extends T> U save( U entity ){
        return getRepository().save( entity );
    }
}
